define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form, Template, Echarts) {

    var Controller = {
        common: function (obj) {
            $(window).resize(function () {
                for (let i in obj) {
                    obj[i].chart.resize();
                }
            });


            //每隔60s定时刷新图表
            setInterval(function () {
                $(".btn-refresh").trigger("click");
            }, 60000);

            //点击按钮
            $(document).on("click", ".btn-filter", function () {
                var label = $(this).text();
                var obj = $(this).closest("form").find(".datetimerange").data("daterangepicker");
                var dates = obj.ranges[label];
                obj.startDate = dates[0];
                obj.endDate = dates[1];
                obj.clickApply();
            });

            //选项卡切入事件
            $(document).on("click", "#resetecharts", function () {
                setTimeout(function () {
                    $(window).trigger("resize");
                }, 50);
            });

        },
        index: function () {
            let obj = {}
            obj.chart1 = this.api.index.myChart1();

            this.common(obj);
            Form.api.bindevent($("#form1"));

        },

        api:{
            index:{
                myChart1: function (){


                    var date = '';


                    $("#form1 .datetimerange").data("callback", function (start, end) {
                        date = start.format(this.locale.format) + " - " + end.format(this.locale.format);
                        $(this.element).val(date);
                        refresh();
                    });


                    //点击按钮
                    $(document).on("click", ".btn-type", function () {
                        refresh();
                    });


                    var si = null;

                    function refresh() {
                        si && clearTimeout(si);
                        si = setTimeout(function () {
                            Fast.api.ajax({
                                url: 'stat/rank/index',
                                data: {
                                    date: date,
                                },
                                loading: false
                            }, function (res) {
                                $("#list").empty();
                                $.each(res.list, function(name, value) {

                                    //if (value.count>0){
                                        var rank = name+1;
                                        var str = "<tr> " +
                                            "<td>"+rank+"</td> " +
                                            "<td>"+value.hotel_name+"</td> " +
                                            "<td>"+value.count+"</td> " +
                                            "<td>"+value.money+"</td> " +
                                            "</tr>";
                                        $("#list").append(str);
                                    //}

                                });


                                return false;
                            });
                        }, 50);
                    }

                    return {

                        refresh: refresh
                    };
                },

            },

        }
    };
    return Controller;
});
